package com.crl.gen.controller;

import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.validation.DeleteValidGroup;
import com.cars.util.validation.GetValidGroup;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.gen.dto.GenConfigDTO;
import com.crl.gen.service.GenConfigService;
import com.crl.gen.vo.GenConfigSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/22 23:01
 */

@RestController
@RequestMapping("/gen/config")
@Api(tags = "代码生成器-公共配置")
public class GenConfigController {

    @Autowired
    private GenConfigService genConfigService;

    /**
     * 公共配置列表
     *
     * @param genConfigSearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "公共配置列表")
    public JsonResult list(@RequestBody GenConfigSearchVO genConfigSearchVO) {
        List<GenConfigDTO> list = genConfigService.list();
        return JsonResultUtil.ok(list);
    }


    /**
     * 新增公共配置
     *
     * @param genConfigDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增公共配置")
    public JsonResult add(@RequestBody @Valid GenConfigDTO genConfigDTO) {
        int flag = genConfigService.add(genConfigDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 修改公共配置
     *
     * @param genConfigDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改公共配置")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody GenConfigDTO genConfigDTO) {
        int flag = genConfigService.update(genConfigDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除公共配置
     *
     * @param genConfigSearchVO
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除公共配置")
    public JsonResult delete(@Validated({DeleteValidGroup.class}) @RequestBody GenConfigSearchVO genConfigSearchVO) {
        int flag = genConfigService.delete(genConfigSearchVO.getId());
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取单个公共配置
     *
     * @param genConfigSearchVO
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个公共配置")
    public JsonResult get(@Validated({GetValidGroup.class}) @RequestBody GenConfigSearchVO genConfigSearchVO) {
        GenConfigDTO genConfigDTO = genConfigService.get(genConfigSearchVO.getId());
        return JsonResultUtil.ok(genConfigDTO);
    }
}

